local sk = fk.CreateSkill {
  name = "hx__AWEC_give",
}

Fk:loadTranslationTable {
  ["hx__AWEC_give"] = "出牌",
  ["hx__AWEC_at"] = "压牌",
  ["#hx__AWEC_at"] = "让每一列的牌花色相同且第二行的牌的点数更大（主无视花色，大小王无视点数和花色）",
}

--根据卡牌id获取点数
---@param id integer @ 卡牌id
---@return integer @ 点数
local function getNumber(id)
  local card = Fk:getCardById(id)
  if card.number == 1 then  --A比K大
    return 14
  elseif card.number == 0 then  --大小王比A大
    return 15
  end
  return card.number
end

--根据卡牌id获取花色
---@param id integer @ 卡牌id
---@return integer @ 花色
local function getSuit(id)
  local card = Fk:getCardById(id)
  local Suit = Fk:currentRoom():getBanner("hx__AWEC_mode-Suit") or Card.NoSuit
  if card.name == "hx__joker" or card.name == "hx__joker2" then
    return Suit
  end
  return card.suit
end

--压牌原理
local function checkCard(tocards, lordcards, Suit)
  local can = 0
  if #tocards > #lordcards then
    return false
  end
  for i = #tocards, 1, -1 do
    local tosuit = getSuit(tocards[i])
    local tonumber = getNumber(tocards[i])
    local losuit = getSuit(lordcards[i])
    local lonumber = getNumber(lordcards[i])
    if losuit == Card.NoSuit then  --大小王无视所有花色
      can = can + 1
      goto continue
    end
    if losuit == Suit then  --主牌无视花色
      if tosuit ~= Suit then  --要压的牌不是主牌
        can = can + 1
        goto continue
      else --是主牌则需点数更大
        if lonumber > tonumber then
          can = can + 1
          goto continue
        else
          return false
        end
      end
    else --不用主牌时
      if lonumber > tonumber and losuit == tosuit then  --默认规则，点数大且花色同
        can = can + 1
        goto continue
      else
        return false
      end
    end
    ::continue::
  end
  return can == #tocards
end

Fk:addPoxiMethod {
  name = "hx__AWEC_at",
  prompt = "#hx__AWEC_at",
  card_filter = function(to_select, selected, data, extra_data)
    if table.contains(data[1], selected[1]) and table.contains(data[2], to_select) then return false end
    if table.contains(data[2], selected[1]) and table.contains(data[1], to_select) then return false end
    local Suit = Fk:currentRoom():getBanner("hx__AWEC_mode-Suit") or Card.NoSuit
    local tocards = data[1]
    local lordcards = table.slice(data[2], 1 , #data[1] + 1)
    return checkCard(tocards, lordcards, Suit)
  end,
  feasible = function(selected, data, extra_data)
    local Suit = Fk:currentRoom():getBanner("hx__AWEC_mode-Suit") or Card.NoSuit
    local tocards = data[1]
    local lordcards = table.slice(data[2], 1 , #data[1] + 1)
    return checkCard(tocards, lordcards, Suit)
  end,
  default_choice = function (data, extra_data)
    return {}
  end,
}

sk:addEffect("active", {
  mute = true,
  min_card_num = 1,
  target_num = 0,
  can_use = Util.TrueFunc,
  card_filter = function(self, player, to_select, selected)
    local room = Fk:currentRoom()
    local lordid = room:getBanner("hx__AWEC_mode-Lord")
    local lord = room:getPlayerById(lordid)
    return #selected < lord:getHandcardNum() and Fk:getCardById(to_select).name ~= "hx__joker" and Fk:getCardById(to_select).name ~= "hx__joker2"
    and table.every(selected, function (id) return Fk:getCardById(id):compareNumberWith(Fk:getCardById(to_select)) end)
  end,
})

return sk
